package com.attendance.chdingtask.utils.dingApi;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiUserListidRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubidRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiUserListidResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubidResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.taobao.api.ApiException;

import java.util.List;

public class UserHelper {
    public static String getUserIdInfo(String accessToken, String code) {
        return null;
    }

    public static OapiV2UserGetResponse.UserGetResponse getUserInfoDetail(String accessToken, String userId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
        req.setUserid(userId);
        req.setLanguage("zh_CN");
        OapiV2UserGetResponse rsp = client.execute(req, accessToken);
        if(!rsp.isSuccess()){
            throw new ApiException("员工信息获取异常"+rsp.getErrmsg()+"-------员工的人员id为"+userId+"-------------");
        }
        return rsp.getResult();
    }

    public static OapiUserListidResponse.ListUserByDeptResponse getUserIdLists(String accessToken, Long deptId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
        OapiUserListidRequest req = new OapiUserListidRequest();
        req.setDeptId(deptId);
        OapiUserListidResponse rsp = client.execute(req, accessToken);
        if(!rsp.isSuccess()){
            throw new ApiException("获取员工id组异常"+rsp.getErrmsg());
        }
        return rsp.getResult();
    }

    public static void main(String[] args) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
        OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest();
        req.setDeptId(1L);
        OapiV2DepartmentListsubidResponse rsp = client.execute(req, "a5271913adf33a6f85b0f5c865175a75");
        OapiV2DepartmentListsubidResponse.DeptListSubIdResponse listSubIdResponse=rsp.getResult();
        List<Long>  depts=listSubIdResponse.getDeptIdList();

        System.out.println(rsp.getBody());
    }
}
